#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

#include "../../../../core/shader_binding_ID.h"
#include "../../../../core/shader_std.h"
#include "../../../../core/shader_mesh.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_taster_transform.h"
#include "../../../../core/shader_taster_color.h"
#include "../../../../core/shader_taster_line.h"
#include "../../../../core/shader_taster_bone.h"
#include "../../../../core/shader_ray.h"



//layout(std140, set=0, binding=2) uniform U3_Matrix {
//	vec4 mat_LightDir[4];	
//};

layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inUV;


layout(location = 0) out vec4 vColor;
layout(location = 1) out vec3 vNormal;
layout(location = 2) out vec3 vCamera[4];
//layout(location = 6) out float vDepthScale;
layout(location = 8) out uint vPrimitiveID;
layout(location = 9) out uint vMeshOffset;
layout(location = 20) out uint vFaceOffset;
//layout(location = 18) out S_SceneDataCall callSceneData;

layout(location = DEF_BINDING_LocationBindID_Vert) out vec3 vVert;
layout(location = DEF_BINDING_LocationBindID_FaceAttr) out S_Geom_MeshDrawProp vFaceAttr;
layout(location = DEF_BINDING_LocationBindID_RotMat) out mat4 vTranformMat;

//layout(location = DEF_BINDING_TasterBindID_MeshAtt ) out flat uint vMeshAtt_Offset;
//layout(location = DEF_BINDING_TasterBindID_MaterialAtt) out flat uint vMaterialAtt_Offset;



out gl_PerVertex {
	vec4 gl_Position;
	float gl_PointSize;
};



void main(void){
	//uint InstanceIndex = gl_InstanceIndex + drawProp.MatrixOffset;
	//vInstanceIndex = gl_BaseInstance;
	mat4 bonesMat_som = mat_Model[gl_InstanceIndex + drawProp.MatrixOffset];
	vColor = f_glsl_buildColor(InstanceColor[gl_InstanceIndex + drawProp.ColorPropOffset]);
	//mat4 bonesMat_som = mat_Model[gl_InstanceIndex];
	//vColor = f_glsl_buildColor(InstanceColor[gl_InstanceIndex]);

	vTranformMat = bonesMat_som;
	vNormal = normalize(mat3x3(vTranformMat) * inNormal);

	//固定顶点坐标
	vec4 p = vec4(inPos, 1);
	S_MeshTranformProp prop = m_LinesProp[drawProp.MeshPropOffset + gl_DrawID];
	vVert = p.xyz;

	//vDepthScale = prop.m_DepthScale;

	
	switch(drawProp.DrawType){
		case DEF_DrawType_LS:
			break;
		case DEF_DrawType_Weigth:
			vColor.x = 0;
			break;
		case DEF_DrawType_Normal_To_Col:
			vColor = vec4(inNormal, 1);
			break;
	}
	
	vec3 camPos = (inverse(mat_ProjecView[3]) * vec4(0,0,0,1)).xyz;

	if ( f_lineMeshTranform(p, inNormal, prop, bonesMat_som, (mat3(mat_ProjecView[1])), camPos) ) {
		gl_Position = mat_ProjecView[0] * p;
		return;
	}
	
	
	vec3 a = p.xyz;
	vec3 n = vNormal;
	//float weigthColor = 0;
	if ( drawProp.BoneNum > 0) {
		uint VertexIndex = gl_VertexIndex - drawProp.VertexOffset;
		S_VertBone VertBone = m_VertBone[drawProp.BonesOffset + VertexIndex];
		//S_VertBone VertBone = m_VertBone[gl_VertexIndex];
		for (uint i=0; i<VertBone.num; ++i) {
			S_BoneMap BoneWeigth = m_BonesAttrOffset[VertBone.bone_offset + i + drawProp.BoneWeigetOffset];
			
			S_BonePose BonePose = m_SkeletonMatrix[BoneWeigth.boneID + drawProp.BonePoseOffset];

			//固定顶点到初始骨骼坐标
			vec3 p_pos = p.xyz - BonePose.tranformBoneA.xyz;

			p_pos = f_graph_quatTranformInv(BonePose.quat, p_pos) + BonePose.tranformBoneB.xyz;
			a = mix(a, p_pos, i > 0 ? BoneWeigth.weigth : 1);
			//a = mix(a, p_pos, BoneWeigth.weigth);
			//n = mix(n, newNormal, weigth);
			if(BonePose.select > 0){
				vColor.x = max(BoneWeigth.weigth, vColor.x);
			}
		}
	}
	p.xyz = a;
	vNormal = n;
	
	vFaceAttr.Offset_Face_AdjacencyID = drawProp.Offset_Face_AdjacencyID;
	vFaceAttr.Offset_Face_Normal = drawProp.Offset_Face_Normal;
	vFaceAttr.Offset_Face_Center = drawProp.Offset_Face_Center;
	vFaceAttr.Offset_MeshAttr = drawProp.Offset_MeshAttr;
	vFaceAttr.Offset_MaterialAttr = drawProp.Offset_MaterialAtt;
	vFaceAttr.Offset_Face = drawProp.Offset_Face;

	vPrimitiveID = gl_VertexIndex;
	vMeshOffset = drawProp.ObMesh_Offset;
	vFaceOffset = drawProp.ElementOffset;
	//vFaceOffset = 0;

	vCamera[0] = normalize(inverse(mat3(mat_ProjecView[1])) * vec3(0.0, 0.0, DEF_ForwardDir));
	vCamera[1] = (inverse(mat_ProjecView[3]) * vec4(0, 0, 0, 1)).xyz;
	

	//vMaterialID = 0;


	gl_Position = mat_ProjecView[0] * p;
	gl_PointSize = 1;
	//switch(prop.m_Activate){
	//	case 1:
	//		gl_Position.z += 0.1 / length(gl_Position.xyz - vCamera[0]);
	//		break;
	//default:
	//	//gl_Position.y += 0.1;
	//	break;
	//}
}


 












